﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.IO;

namespace UI.Web.MVC.Code.Common
{
    public static class DatabaseHelper
    {
        /// <summary>
        /// Створюємо базу даних
        /// </summary>
        /// <param name="databseName">Назва бази даних</param>
        /// <returns>Створено || Не стоворено</returns>
        public static bool CreateDataBase(string databseName)
        {
            //var info = new DirectoryInfo(string.Format(@"C:\DataBases\{0}", databseName + "_PROTOTYPE"));
            //info.Create();
            var sqlConnection = new SqlConnection("server=10.7.1.10;user=sa;password=123456;database=master;");
//            string str = string.Format(@"CREATE DATABASE {0} ON PRIMARY (NAME = {0}_Data, FILENAME = 'C:\Databases\{0}\{0}Data.mdf',
//                              SIZE = 5MB, MAXSIZE = UNLIMITED, FILEGROWTH = 10%) LOG ON (NAME = {0}_Log,
//                              FILENAME = 'C:\Databases\{0}\{0}.ldf', SIZE = 5MB, MAXSIZE = 2048GB, FILEGROWTH = 10%)",
//                              databseName.ToUpper() + "_PROTOTYPE");
            string str = string.Format("CREATE DATABASE {0}_PROTOTYPE", databseName); 

            var myCommand = new SqlCommand(str, sqlConnection);
            try
            {
                sqlConnection.Open();
                myCommand.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                return false;
            }
            finally
            {
                if (sqlConnection.State == ConnectionState.Open)
                {
                    sqlConnection.Close();
                }
            }
            return true;
        }
    }
}